package com.yanchangapp.ui.presenter;

import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.bean.LookLinkManBean;
import com.yanchangapp.model.request.MakePlanManRequest;
import com.yanchangapp.model.request.MakePlanWorkRequest;
import com.yanchangapp.model.response.RelatedManResponse;
import com.yanchangapp.ui.view.LookLinkManAtView;
import com.zxl.baselib.bean.response.BaseListResponse;
import com.zxl.baselib.bean.response.BaseResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019-10-12.
 * description:
 */
public class LookLinkManAtPresenter extends BasePresenter<LookLinkManAtView> {

    public LookLinkManAtPresenter(BaseActivity context) {
        super(context);
    }

    public void queryLinkManLists(long id){
        HeadApiRetrofit.getInstance().getQueryLookMan(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseListResponse<RelatedManResponse>>() {
                    @Override
                    public void onNext(BaseListResponse<RelatedManResponse> relatedManResponseBaseListResponse) {
                        if (getView() != null){
                            if (AppConstant.REQUEST_SUCCESS == relatedManResponseBaseListResponse.getCode()){
                                returnListData(relatedManResponseBaseListResponse.getData());
                            }else {
                                getView().queryLookLinkManFailure(relatedManResponseBaseListResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().queryLookLinkManFailure("网络错误");
                        }
                    }
                });
    }

    private void returnListData(List<RelatedManResponse> data) {
        List<LookLinkManBean> manBeans = new ArrayList<>();
        if (data != null && data.size() != 0){
            for (int i= 0;i<data.size();i++){
                RelatedManResponse relatedManResponse = data.get(i);
                if (relatedManResponse.getEmployeeList() != null){
                    for (int j=0;j<relatedManResponse.getEmployeeList().size();j++){
                        LookLinkManBean lookLinkManBean = relatedManResponse.getEmployeeList().get(j);
                        lookLinkManBean.setLevelName(relatedManResponse.getLevelName());
                        manBeans.add(lookLinkManBean);
                    }
                }
            }
        }
        getView().queryLookLinkManSuccess(manBeans);
    }

    public void actionMakePlan(MakePlanWorkRequest makePlanManRequest){
        getView().showLoadingDialog();
        HeadApiRetrofit.getInstance().postMakePlanFromWork(makePlanManRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        if (getView() != null){
                            getView().hideLoadingDialog();
                            if (AppConstant.REQUEST_SUCCESS == baseResponse.getCode()){
                                getView().makePlanSuccess("分配工作成功");
                            }else {
                                getView().makePlanFailure(baseResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().hideLoadingDialog();
                            getView().makePlanFailure("网络错误");
                        }
                    }
                });
    }
}
